from fuzzywuzzy import fuzz, process


# 看单词是否相同，不管顺序
def calsimilarity1(str1, str2):
    return fuzz.token_sort_ratio(str1, str2)


# 相比fuzz.token_sort_ratio不考虑词语出现的次数；
def calsimilarity2(str1, str2):
    return fuzz.token_set_ratio(str1, str2)


# 完全匹配
def calsimilarity3(str1, str2):
    return fuzz.ratio(str1, str2)


# 部分匹配，如果str1是str2的子串则返回100
def calsimilarity4(str1, str2):
    return fuzz.partial_ratio(str1, str2)


# 从list中找出与str最相似的limit个
def extractmostn(str1, lists):
    return process.extract(str1, lists)


# 从list中找出最相似的一个字符串，返回的是（resstr, score）,答案字符串和对应的分数
def extractone(str1, lists):
    # process.extractOne(str1, lists)[0]可以得到最相似的字符串
    return process.extractOne(str1, lists)


if __name__ == "__main__":
    # print("1: "+str(calsimilarity1("微振磨损", "磨损")))
    # print("2: "+str(calsimilarity1("磨损", "微振磨损")))
    # print("3: "+str(calsimilarity1("损磨", "微振磨损")))
    # print("1: "+str(calsimilarity2("微振磨损", "磨损")))
    # print("2: "+str(calsimilarity2("磨损", "微振磨损")))
    # print("3: "+str(calsimilarity2("损磨", "微振磨损")))
    # print("1: "+str(calsimilarity3("微振磨损", "磨损")))
    # print("2: "+str(calsimilarity3("磨损", "微振磨损")))
    # print("3: "+str(calsimilarity3("损磨", "微振磨损")))
    # print("1: "+str(calsimilarity4("微振磨损", "磨损")))
    # print("2: "+str(calsimilarity4("磨损", "微振磨损")))
    # print("3: "+str(calsimilarity4("损磨", "微振磨损")))
    list1 = ["点蚀", "疲劳剥落", "磨损", "损磨"]
    # for s in extractmostn("微震磨损", list1, 1):
    #     print(s)
    list2 = ["点蚀", "剥落", "化学腐蚀", "电腐蚀"]
    # for s in extractmostn("出现了腐蚀", list2):
    #     print(s)
    print(extractone("出现了腐蚀", list2)[0])
